package com.ht.sps.util;

import org.apache.commons.lang3.ArrayUtils;

/**
 * Created by bcw on 14-3-5.
 */
public class GlobalAddressUtil {
    public static String getGlobalAreaControllerAddress(int acAddress) {
        return "2."+acAddress+".0.0";
    }

    public static String getGlobalDoorControllerAddress(int acAddress, int dcAddress) {
        return "3."+acAddress+"."+dcAddress+".0";
    }

    public static String getGlobalRelayAddress(int acAddress, int dcAddress, int doorNumber) {
        return "6."+acAddress+"."+dcAddress+"."+doorNumber;
    }

    public static String getGlobalReaderAddress(int acAddress, int dcAddress, int number) {
        return "4."+acAddress+"."+dcAddress+"."+number;
    }

    public static String getGlobalInputAddress(int acAddress, int dcAddress, int number) {
        return "5."+acAddress+"."+dcAddress+"."+number;
    }

    public static int[] parseGlobalDoorAddress(String address) {
        if (!address.startsWith("6.")) {
            return null;
        }

        int[] ret = parseGlobalAddress(address);

        if (ret == null || ret.length != 4) {
            return null;
        }

        return ArrayUtils.subarray(ret, 1, 4);
    }

    public static int[] parseGlobalAddress(String address) {
        if (address == null || address.isEmpty()) {
            return null;
        }
        String[] array = address.split("\\.");
        if (array.length != 4) {
            return null;
        }
        int[] ret = new int[4];

        ret[0] = Integer.parseInt(array[0]);// 类型
        ret[1] = Integer.parseInt(array[1]);// 区域控制器地址
        ret[2] = Integer.parseInt(array[2]);// 门控制器地址
        ret[3] = Integer.parseInt(array[3]);// 门、读卡器、继电器、输入序号

        return ret;
    }
}
